package de.homelabs.bookstore.web.ui;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
import org.springframework.web.context.WebApplicationContext;

@Service("hlMenuManager")
public class HLMenuManager implements ApplicationContextAware {

	List<MenuEntry> mainMenuEntries = new ArrayList<MenuEntry>();
	List<MenuEntry> subMenuEntries	= new ArrayList<MenuEntry>();
	
	
	private ApplicationContext appContext;
	
	public HLMenuManager(){
		
		
	}
	
	public List<MenuEntry> getMainMenuEntries(){
		return this.mainMenuEntries;
	}
	
	public List<MenuEntry> getSubMenuEntries(){
		return this.subMenuEntries;
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.appContext = applicationContext;
		System.out.println(appContext);
		
		if (appContext != null && appContext instanceof WebApplicationContext){
			WebApplicationContext wc = (WebApplicationContext) appContext;
			String contextPath = wc.getServletContext().getContextPath();
			
			//Main Menu
			mainMenuEntries.add(new MenuEntry("Home",contextPath+"/index.htm"));
			mainMenuEntries.add(new MenuEntry("News",contextPath+"/news.htm"));
			mainMenuEntries.add(new MenuEntry("Impressum",contextPath+"/impressum.htm"));
			mainMenuEntries.add(new MenuEntry("About",contextPath+"/modul/about.htm"));
		} else {
			//fehler
		}
	}
}
